#include "team.h"
#include <player.h>
#include <netpacket.h>

Team::Team ():memberCount(0)
{

	//Test
	//for(int i =0;i<MAX_MEMBER;i++)
	//{
	//	NAME n;
	//	n = i;
	//	addMember(n,100*i,100*i+500,100*i-50*i,100*i-50*i+500,10,i);
	//}
}

long long Team::getLeaderID ()
{
	if (memberCount > 0)
	{
		return vMember[0].uid ;
	}else
		return -1 ;
}

bool Team::hasMember (long long uid)
{
	for (int i = 0; i<memberCount; i++)
	{
		if (vMember[i].uid == uid)
			return true ;
	}
	return false ;
}


bool Team::addMember (NAME& nm, int hp, int mhp, int mp, int mmp, int lv, long long id)
{
	if (memberCount < MAX_MEMBER)
	{
		vMember[memberCount].name = nm ;
		vMember[memberCount].hp = hp ;
		vMember[memberCount].max_hp = mhp ;
		vMember[memberCount].mp = mp ;
		vMember[memberCount].max_mp = mmp ;
		vMember[memberCount].lv = lv ;
		vMember[memberCount].uid = id ;
		memberCount ++ ;

		return true;
	}
	else
	{
		return false;
	}
}

void Team::setMemberData(long long UniqueID, PacketTeamDataChange& pkg)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
		{
			vMember[i].lv = pkg.numerical[PacketTeamDataChange::TEAM_LV];
			vMember[i].hp = pkg.numerical[PacketTeamDataChange::TEAM_HP];
			vMember[i].max_hp = pkg.numerical[PacketTeamDataChange::TEAM_MAXHP] + pkg.numerical[PacketTeamDataChange::TEAM_TOTALHP];
			vMember[i].mp = pkg.numerical[PacketTeamDataChange::TEAM_MP];
			vMember[i].max_mp = pkg.numerical[PacketTeamDataChange::TEAM_MAXMP] + pkg.numerical[PacketTeamDataChange::TEAM_TOTALMP];
		}
}

void Team::setMemberLV(long long UniqueID, int lv, int hp, int mhp, int mp, int mmp)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
		{
			vMember[i].lv = lv;
			vMember[i].hp = hp;
			vMember[i].max_hp = mhp;
			vMember[i].mp = mp;
			vMember[i].max_mp = mmp;
		}
}

void Team::setMemberLV(long long UniqueID, int lv)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
			vMember[i].lv = lv;
}

void Team::setMemberHP(long long UniqueID, int hp)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
			vMember[i].hp = hp;
}

void Team::setMemberMAXHP(long long UniqueID, int mhp)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
			vMember[i].max_hp = mhp;
}

void Team::setMemberMP(long long UniqueID, int mp)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
			vMember[i].mp = mp;
}

void Team::setMemberMAXMP(long long UniqueID, int mmp)
{
	for (int i = 0; i < memberCount; i++)
		if (vMember[i].uid == UniqueID)
			vMember[i].max_mp = mmp;
}

void Team::delMember (long long id)
{
	for (int i = 0; i<memberCount; i++)
	{
		if (vMember[i].uid == id)
		{
			memberCount -- ;
			
			if(i<memberCount)
			{
				for (int m = i; m<memberCount; m++)
				{
					vMember[m] = vMember[m+1] ;

					if(m==memberCount-1)
					{
						clearMember((long long ) m+1);
					}
				}
				break ;
			}
			else if(i == memberCount)
			{
				clearMember((long long) i);
				break;
			}
		}
	}
}

void Team::clearMember(long long id)
{
	vMember[id].name = "0" ;
	vMember[id].hp = -1 ;
	vMember[id].max_hp = -1;
	vMember[id].mp = -1 ;
	vMember[id].max_mp = -1;
	vMember[id].lv = -1 ;
	vMember[id].uid = -1 ;
}